package com.zzptc.zzsgz.bdsjws.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.R;
import com.zzptc.zzsgz.bdsjws.activities.AddContactsActivity;
import com.zzptc.zzsgz.bdsjws.adapter.SOSAdapter;
import com.zzptc.zzsgz.bdsjws.fragments.base.BaseFragmentbase;
import com.zzptc.zzsgz.bdsjws.vo.Contact;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by zzsgz on 2017/5/27.
 */

public class SecondFragment extends BaseFragmentbase {


    @BindView(R.id.et_add)
    EditText etAdd;
    @BindView(R.id.btn_yes)
    Button btnYes;
    @BindView(R.id.rv_sos_tel)
    RecyclerView rvSosTel;
    @BindView(R.id.img_add)
    ImageView imgAdd;
    //Unbinder unbinder;


    @Override
    public void initView() {
        etAdd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                btnYes.setVisibility(View.VISIBLE);
                imgAdd.setVisibility(View.GONE);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

    }

    @Override
    public void initData() {

    }

    @Override
    public int getXML() {
        return R.layout.second_fragment;
    }


    @OnClick(R.id.btn_yes)
    public void onYesClicked() {
        //先判断电话号码的长度  并且要获取电话号码
        String tel = etAdd.getText().toString().trim();
        if (tel.length() == 11 && tel.matches("1[3578]\\d{9}$")) {
            List<Contact> listSos = MyApplication.getSosList();
            Contact contact = new Contact();
            contact.setPhonenum(tel);
            if (listSos.size()<3){
                    listSos.add(contact);
            }else {
                Toast.makeText(getActivity(), "已经有三个人了", Toast.LENGTH_SHORT).show();
            }
            //适配器   数据源   布局管理器
            SOSAdapter sosAdapter = new SOSAdapter(listSos);
            rvSosTel.setLayoutManager(new LinearLayoutManager(getActivity()));
            rvSosTel.setAdapter(sosAdapter);
        }
        //判断电话号码的长度
        etAdd.setText("");
        btnYes.setVisibility(View.GONE);
        imgAdd.setVisibility(View.VISIBLE);

    }

//    @Override
//    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        // TODO: inflate a fragment view
//        View rootView = super.onCreateView(inflater, container, savedInstanceState);
//        unbinder = ButterKnife.bind(this, rootView);
//        return rootView;
//    }
//
//    @Override
//    public void onDestroyView() {
//        super.onDestroyView();
//        unbinder.unbind();
//    }

    @OnClick(R.id.img_add)
    public void onViewClicked() {
        startActivity(new Intent(getActivity(), AddContactsActivity.class));

    }


}
